package cn.nawang.ebeim.api.repository.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.nawang.ebeim.api.bean.ClassBean;
import cn.nawang.ebeim.api.comm.BaseQuery;
import cn.nawang.ebeim.api.comm.PageList;
import cn.nawang.ebeim.api.comm.Pages;
import cn.nawang.ebeim.api.comm.SearchFormParams;
import cn.nawang.ebeim.api.domain.UserBeHaviorsLog;
import cn.nawang.ebeim.api.utils.Constant;
import cn.nawang.ebeim.api.utils.RefectSql;

@Repository
public class UserBehaviorsLogRepositoryImpl extends BaseQuery<UserBeHaviorsLog> {
    @Autowired
    private EntityManagerFactory entityManagerFactory;
 
    public List<UserBeHaviorsLog> findByBackupLog(UserBeHaviorsLog userBeHaviorsLog){
      List<UserBeHaviorsLog> objList = null;
      StringBuffer sql = new StringBuffer("from UserBeHaviorsLog where 1=1");
//      List<ClassBean> conditions = RefectSql.getClassBeans(userBeHaviorsLog);
//    
//      for (int i = 0; i < conditions.size(); i++) {
//        ClassBean classBean = conditions.get(i);
//        if (classBean.getContent() != null) {
//          sql.append(" and ");
//          sql.append(classBean.getName());
//          sql.append(" = ");
//          sql.append(classBean.getContent());
//        }
//      }
      EntityManager em = entityManagerFactory.createEntityManager();
      try {      
        Query query = em.createQuery(sql.toString());
        objList = query.getResultList();
      } catch (Exception ex) {
        ex.printStackTrace();
      }finally{
        em.close();
      }
      return objList;
  }
  
    public  PageList<UserBeHaviorsLog> queryListByPage(PageList<UserBeHaviorsLog> pl,String sqlCount, String sqlList, Pages page) {
      
        EntityManager em = entityManagerFactory.createEntityManager();
        try {
         

          // 数据查询
          Query query = em.createQuery(sqlList);
          query.setFirstResult(page.getStartPos());
          query.setMaxResults(page.getPageSize());
          List<UserBeHaviorsLog> list = query.getResultList();      
          pl.setObjList(list);
          
          // 查询统计
          Query queryCount = em.createQuery(sqlCount);
          List<Object> lc = queryCount.getResultList();
          int count = 0;
          if (lc != null && !lc.isEmpty()) {
            count = ((Long) lc.get(0)).intValue();
          }
          page.setTotalPage(page.getAllPage(count));
          page.setTotalRecord(count);
          
          pl.setPage(page);
          pl.setCode(Constant.CODE_SUCCESS);
          pl.setDesc("数据查询成功");
        } catch (Exception ex) {
          ex.printStackTrace();
          pl.setCode(Constant.CODE_ERROR);
          pl.setDesc("服务器忙，请稍后再试。");
        }finally{
          em.close();
        }
        return pl;
      } 
    
    
  public PageList<UserBeHaviorsLog> findByPage(UserBeHaviorsLog userBeHaviorsLog,Pages page){
      
      PageList<UserBeHaviorsLog> pageResult = new PageList<UserBeHaviorsLog>();
      StringBuffer hqlCount = new StringBuffer("select count(1) from UserBeHaviorsLog bl where 1=1 ");
      StringBuffer hqlList= new StringBuffer("select new UserBeHaviorsLog(id,action,dsId,beginTime,elapsedTime,userID,targetPath,result)"
              + " from UserBeHaviorsLog bl where userID = " + userBeHaviorsLog.getUserID());
                  
      hqlList.append(" order by bl.beginTime desc");
      page.setSearchFormParams(new SearchFormParams[0]);
      try{
          pageResult = queryListByPage(pageResult,hqlCount.toString(), hqlList.toString(), page);
      }catch (Exception ex) {
        ex.printStackTrace();
      }
      return pageResult;
  }
}
